package cn.q3c.smzdm.dao;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.springframework.stereotype.Service;

import cn.keyvalue.ccf.base.BaseDAO;
import cn.q3c.smzdm.bean.Crawler;
import cn.q3c.smzdm.bean.Images;

@Service
public class CrawlerDAO extends BaseDAO{

	public long addCrawler(Crawler crawler) throws SQLException {

		Connection conn = null;

		long id = -1;
		String sql = "insert into crawler(`url`,`title`,`description`,`keywords`,`content`,`cdate`,`udate`,`source`) values(?,?,?,?,?,?,?,?)";

		try {

			conn = dataSource.getConnection();

			Object[] objs = new Object[] { 
					crawler.getUrl(),
					crawler.getTitle(),
					crawler.getDescription(),
					crawler.getKeywords(),
					crawler.getContent(),
					crawler.getCdate(),
					crawler.getUdate(),
					crawler.getSource()};
			this.update(conn, sql, objs);

			id = getLastInsertID(conn);

			DbUtils.close(conn);
		} catch (SQLException e) {
			DbUtils.close(conn);
			e.printStackTrace();
		}

		return id;
	}
	
	
	
	public Images getImages(String imageurl) throws SQLException {

		Connection conn = dataSource.getConnection();

		String sql = "select * from images where imageurl=?";

		Images result = null;
		try {

			Object[] objs = new Object[] { imageurl };

			result = (Images) this.query(conn, sql,
					new BeanHandler<Images>(Images.class), objs);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.close(conn);
		}

		return result;
	}
	
	
	
	public long addImages(Images image) throws SQLException {

		Connection conn = null;

		long id = -1;
		String sql = "insert into images(`imageurl`,`source`,`localpath`,`cdate`,`udate`) values(?,?,?,?,?)";

		try {

			conn = dataSource.getConnection();

			Object[] objs = new Object[] { 
					image.getImageurl(),
					image.getSource(),
					image.getLocalpath(),
					image.getCdate(),
					image.getUdate()
					};
			this.update(conn, sql, objs);

			id = getLastInsertID(conn);

			DbUtils.close(conn);
		} catch (SQLException e) {
			DbUtils.close(conn);
			e.printStackTrace();
		}

		return id;
	}
}